package it.polito.connectedcar.message;

import it.polito.connectedcar.util.Constants;

/**
 * This message subclass deals with messages coming from trafficLights entities.
 * -A trafficLiht sends its ID in order to be recognized. -The currentPhaseIndex
 * to let receivers be able to predict which is the following phase and to
 * behave consequently. -The assumedTimeToNextSwitch which is the absolute time
 * when TrafficLight will change its current phase.
 * 
 * @author Nicola Aresta
 * 
 */
public class TCCFromTrafficLightMessage extends IncomingMessage {

	private String trafficLightId;
	private int currentPhaseIndex;
	private long lastChangeTimestamp;
	private long assumedTimeToNextPhaseSwitch;

	public TCCFromTrafficLightMessage(byte[] byteArray) {
		super(byteArray, Constants.TRAFFIC_LIGHT_TYPE_CODE);
		actionCode = Constants.TRAFFIC_LIGHT_UPDATE;
	}

	@Override
	protected boolean onProcessMessage(Storage st) {
		if (actionCode != Constants.TRAFFIC_LIGHT_UPDATE|| sourceTypeCode != Constants.TRAFFIC_LIGHT_TYPE_CODE)
			return false;

		this.trafficLightId = st.readStringASCII();
		this.currentPhaseIndex = st.readInt();
		this.lastChangeTimestamp = st.readLong();
		this.assumedTimeToNextPhaseSwitch = st.readLong();
		return true;
	}

	@Override
	protected void convertFromData(Storage st) {
		this.trafficLightId = st.readStringASCII();
		this.currentPhaseIndex = st.readInt();
		this.lastChangeTimestamp = st.readLong();
		this.assumedTimeToNextPhaseSwitch = st.readLong();

	}

	public String getTrafficLightId() {
		return trafficLightId;
	}

	public void setTrafficLightId(String trafficLightId) {
		this.trafficLightId = trafficLightId;
	}

	public int getCurrentPhaseIndex() {
		return currentPhaseIndex;
	}

	public void setCurrentPhaseIndex(int currentPhaseIndex) {
		this.currentPhaseIndex = currentPhaseIndex;
	}

	public long getAssumedTimeToNextPhaseSwitch() {
		return assumedTimeToNextPhaseSwitch;
	}

	public void setAssumedTimeToNextPhaseSwitch(
			long assumedTimeToNextPhaseSwitch) {
		this.assumedTimeToNextPhaseSwitch = assumedTimeToNextPhaseSwitch;
	}

	public long getLastChangeTimestamp() {
		return lastChangeTimestamp;
	}

	public void setLastChangeTimestamp(long lastChangeTimestamp) {
		this.lastChangeTimestamp = lastChangeTimestamp;
	}
}
